package module.main
{
	import code.obj.IconObj;
	import code.utils.MaterialUtil;
	
	import com.darwin.darwinBitmapEngine.view.SceneView;
	import com.darwin.darwinDll.Dll;
	import com.darwin.darwinIsometric.core.IsoWorld;
	import com.darwin.darwinIsometric.core.Point3D;
	import com.darwin.darwinMVC.core.CommonData;
	import com.darwin.darwinMVC.mvc.Command;
	import com.darwin.darwinUI.manager.UIManager;
	import com.darwin.darwinUtils.Priority;
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import main.App;
	import main.AppConfig;
	import main.AppDataSource;
	import main.AppNet;
	import main.mvc.GController;
	
	import module.building.cmds.BuildCommand;
	import module.edittroop.cmds.EditTroopCmdConst;
	import module.edittroop.view.troopinfopanel.TroopInfoPanel;
	import module.main.cmds.MainCmdConst;
	import module.main.event.MainEvent;
	import module.main.model.FriendInfoModelData;
	import module.main.model.ObjectModelData;
	import module.main.model.UserInfoModelData;
	import module.main.model.dataObj.UserInfoObj;
	import module.main.view.MainView;
	import module.main.view.ResourceConst;
	import module.main.view.mainInterface.friendListPanel.FriendListPanel;
	import module.main.view.mainInterface.troopListPanel.TroopListPanel;
	import module.scene.cmds.SceneCommand;
	import module.scene.consts.ActionType;
	import module.shop.cmds.ShopCommandsConst;
	import module.shop.view.ProduceItemEffect;
	import module.shop.view.ResouseAnimation;
	
	public class MainController extends GController
	{
		public function MainController()
		{
			super();
		}

		override public function start():void
		{
			// TODO Auto Generated method stub
			super.start();
			init();
			initEventsListeners();
			addRegister();
		}
		
		private var _mainView:MainView;
		private var friendListIsClose:Boolean = true;
		private var troopListIsClose:Boolean = true;
		private var _troopList:Array;
		
		private function init():void
		{
			_timer = new Timer(1);
			_mainView = new MainView();
			this.appCommonView.mainView = _mainView;
			this.appCommonView.appContainer.uiLayer.addChild(_mainView);
						
			var modelData:ObjectModelData = new ObjectModelData();
			var userInfoModelData:UserInfoModelData = new UserInfoModelData;
			var friendInfoModelData:FriendInfoModelData = new FriendInfoModelData;			
		}
		private function addRegister():void
		{
			register(MainCmdConst.MAIN_CMD_FRIENDINFO, friendInfo);
			register(MainCmdConst.MAIN_CMD_USERINFO,userInfo);
			register(SceneCommand.SCENE_CMD_ACTIONTYPE_CHANGED, onActionTypeChanged);
			register(SceneCommand.SCENE_CMD_TROOPLIST_CHANGED, troopListChanged);
			register(SceneCommand.SCENE_CMD_MOUSE_DOWN, mouseDownHandler);
			register(SceneCommand.SCENE_CMD_DROP_ITEM,resouseHandler);
			register(BuildCommand.RESOUSE_GOLD,test);
		}
		private function test(cmd:Command):void
		{
			//var sprite:Sprite = MaterialUtil.getPreloadMaterial("main",ResourceConst.RESOURCEINFO_GOLD_ICON) as Sprite;
			var sprite:Sprite = getSpriteFromName(cmd.data.type);
			var $point_1:Point = new Point();
			var $point_2:Point = getTagetPoint(cmd.data.type);
			$point_1.x = cmd.data.x - appCommonView.sceneView.followUpLayer.x;
			$point_1.y = cmd.data.y - appCommonView.sceneView.followUpLayer.y; 
			var _produceItem:ProduceItemEffect = new ProduceItemEffect(sprite,cmd.data.num,$point_1.x,$point_1.y,$point_2.x,$point_2.y,cmd.data.type);
/*			appCommonView.sceneView.followUpLayer.addChild(_produceItem.spriteBg);
			_produceItem.start();*/
			var test:ResouseAnimation = new ResouseAnimation(sprite,cmd.data.num,$point_1.x,$point_1.y,cmd.data.type);
			appCommonView.sceneView.followUpLayer.addChild(test);
			appCommonView.sceneView.followUpLayer.addEventListener(MainEvent.GAIN_RESOUSE_EVENT,gainResouseHandler);
		}
		/**
		 *播放收获资源动画 
		 * @param cmd
		 * 
		 */		
		private function resouseHandler(cmd:Command):void
		{
			//var sprite:Sprite = MaterialUtil.getPreloadMaterial("main",ResourceConst.RESOURCEINFO_GOLD_ICON) as Sprite;
			var sprite:Sprite = getSpriteFromName(cmd.data.type);
			var $point_1:Point = new Point();
			var $point_2:Point = getTagetPoint(cmd.data.type);
			$point_1.x = cmd.data.x - appCommonView.sceneView.followUpLayer.x;
			$point_1.y = cmd.data.y - appCommonView.sceneView.followUpLayer.y; 
			var _produceItem:ProduceItemEffect = new ProduceItemEffect(sprite,cmd.data.num,$point_1.x,$point_1.y,$point_2.x,$point_2.y,cmd.data.type);
			appCommonView.sceneView.followUpLayer.addChild(_produceItem.spriteBg);
			_produceItem.start();
		}
		private function getSpriteFromName(name:String):Sprite
		{
			var $resouseSpr:Sprite = new Sprite;
			if(name == "gold")
			{
				$resouseSpr = MaterialUtil.getPreloadMaterial("main",ResourceConst.RESOURCEINFO_GOLD_ICON) as Sprite;
			}
			if(name == "iron")
			{
				$resouseSpr = MaterialUtil.getPreloadMaterial("main",ResourceConst.RESOURCEINFO_IRON_ICON) as Sprite;
			}
			if(name == "food")
			{
				$resouseSpr = MaterialUtil.getPreloadMaterial("main",ResourceConst.RESOURCEINFO_FOOD_ICON) as Sprite;
			}
			if(name == "stone")
			{
				$resouseSpr = MaterialUtil.getPreloadMaterial("main",ResourceConst.RESOURCEINFO_STONE_ICON) as Sprite;
			}
			if(name == "wood")
			{
				$resouseSpr = MaterialUtil.getPreloadMaterial("main",ResourceConst.RESOURCEINFO_WOOD_ICON) as Sprite;
			}
			return $resouseSpr;
		}
		/**
		 *通过资源名，得到该资源所在主界面的坐标 
		 * @param name
		 * @return 
		 * 
		 */		
		private function getTagetPoint(name:String):Point
		{
			var $tagetPoint:Point = new Point;
			if(name == "gold")
			{
				$tagetPoint.x = App.appStage.stageWidth -115;
				$tagetPoint.y = 18;
			}
			if(name == "iron")
			{
				$tagetPoint.x = _mainView.resourceinfPanel.x + 269;
				$tagetPoint.y = 18;
			}
			if(name == "food")
			{
				$tagetPoint.x = _mainView.resourceinfPanel.x + 17;
				$tagetPoint.y = 18;
			}
			if(name == "stone")
			{
				$tagetPoint.x = _mainView.resourceinfPanel.x + 100;
				$tagetPoint.y = 18;
			}
			if(name == "wood")
			{
				$tagetPoint.x =_mainView.resourceinfPanel.x + 184;
				$tagetPoint.y = 18;
			}
			return $tagetPoint;
		}	
		/**
		 * 改变部队列表面板
		 * @param cmd
		 */		
		private function troopListChanged(cmd:SceneCommand):void
		{
			_troopList = cmd.data as Array;		
		}
		
		/**
		 *	鼠标层点击 
		 */		
		private function mouseDownHandler(cmd:SceneCommand):void
		{
			if(!troopListIsClose)
			{
				_mainView.removeChild(troopListPanel);
				troopListIsClose = true;
			}
			if(!friendListIsClose)
			{
				_mainView.removeChild(friendListPanel);
				friendListIsClose = true;
			}
		}
		
		private function initEventsListeners():void{
			if(AppConfig.getInstance().userId){
				_login(AppConfig.getInstance().userId,AppConfig.getInstance().validateKey);
			}else{
				_mainView.addLoginBox();
				_mainView.addEventListener(MainEvent.CLICK_LOGIN_EVENT,startLogin);
			}
			
			_mainView.addEventListener(MainEvent.CLICK_BUILD_EVENT, cmdBuild);
			_mainView.addEventListener(MainEvent.CLICK_TROOPLIST_EVENT, showTroopList);
			_mainView.addEventListener(MainEvent.CLICK_FRIEND_EVENT, showFriendList);
			_mainView.addEventListener(MainEvent.CLICK_HERO_IMAGE_EVENT, clickHeroImage);
			_mainView.addEventListener(MainEvent.CLICK_HOME_BUTTON,onHomeClick);
			_mainView.addEventListener(MainEvent.GAIN_RESOUSE_EVENT,gainResouseHandler);
		}
		private function gainResouseByTimer(e:TimerEvent):void
		{
			if(_type == "gold")
			{
				_mainView.playerObj.gold = _mainView.playerObj.gold + 1;
				_mainView.resourceinfPanel.updataResouseInfo();
			}
			if(_type == "food")
			{
				_mainView.playerObj.food = _mainView.playerObj.food + 1;
				_mainView.resourceinfPanel.updataResouseInfo();
			}
			if(_type == "wood")
			{
				_mainView.playerObj.wood = _mainView.playerObj.wood + 1;
				_mainView.resourceinfPanel.updataResouseInfo();
			}
			if(_type == "stone")
			{
				_mainView.playerObj.stone = _mainView.playerObj.stone + 1;
				_mainView.resourceinfPanel.updataResouseInfo();
			}
			if(_type == "iron")
			{
				_mainView.playerObj.iron = _mainView.playerObj.iron + 1;
				_mainView.resourceinfPanel.updataResouseInfo();
			}
		}
		private var _type:String;
		private var _timer:Timer;
		private function gainResouseHandler(e:MainEvent):void
		{
			_type = e.data.type;
			_timer.repeatCount = _timer.repeatCount + e.data.num;
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE,compelete);
			_timer.addEventListener(TimerEvent.TIMER,gainResouseByTimer);
			_timer.start();
		}
		/**
		 *移除侦听 
		 * @param e
		 * 
		 */		
		private function compelete(e:TimerEvent):void
		{
			((e.currentTarget) as Timer).removeEventListener(TimerEvent.TIMER,gainResouseByTimer);
			((e.currentTarget) as Timer).removeEventListener(TimerEvent.TIMER_COMPLETE,compelete);
			//_timer = null;
		}
		
		private var friendListPanel:FriendListPanel;
		
		private var troopListPanel:TroopListPanel;
		
		
		protected function onHomeClick(event:Event):void
		{
			var $cmd:SceneCommand = new SceneCommand(SceneCommand.SCENE_CMD_LOCATE_HOME);
			dispatch($cmd);
		}
		
		/**
		 *	对部队进行聚焦 
		 */		
		private function clickHeroImage(e:MainEvent):void
		{
			var $cmd:SceneCommand = new SceneCommand(SceneCommand.SCENE_CMD_LOCATE_TROOP);
			$cmd.data = e.data;
			dispatch($cmd);
		}
		
		/**
		 * 	显示部队列表
		 */		
		private function showTroopList(e:MainEvent):void
		{
			if(troopListIsClose)
			{
				if(!friendListIsClose)
				{
					_mainView.removeChild(friendListPanel);
					friendListIsClose = true;
				}
				troopListPanel = new TroopListPanel();
				troopListPanel.data = _troopList;
				troopListPanel.x = _mainView._operateFt.x;
				troopListPanel.y = _mainView._operateFt.y - 288;
				_mainView.addChild(troopListPanel);
				troopListIsClose = false;
			}
			else 
			{
				_mainView.removeChild(troopListPanel);
				troopListIsClose = true;
			}
		}
		
		/**
		 *	显示好友列表 
		 */		
		private function showFriendList(e:MainEvent):void
		{
			if(friendListIsClose)
			{
				if(!troopListIsClose)
				{
					_mainView.removeChild(troopListPanel);
					troopListIsClose = true;
				}
				friendListPanel = new FriendListPanel();
				friendListPanel.data = _mainView.friendList;
				friendListPanel.x = _mainView._operateFt.x - 33;
				friendListPanel.y = _mainView._operateFt.y - 288;
				_mainView.addChild(friendListPanel);
				friendListIsClose = false;
			}
			else
			{
				_mainView.removeChild(friendListPanel);
				friendListIsClose = true;
			}
//			//		显示建筑升级面板	
//			var command:BuildCommand = new BuildCommand(BuildCommand.SHOW_BUILDING_UPGRADE);
//			dispatch(command);
		}
		
		private function cmdBuild(e:MainEvent):void
		{
			var cmd:BuildCommand = new BuildCommand(BuildCommand.CLICK_BUILD_OPEN_BUTTON);
			dispatch(cmd);
		}
		
		private function friendInfo(cmd:Command):void
		{
			var friend_List:Array = cmd.data as Array;
			_mainView.friendList = friend_List;
		}
		
		private function userInfo(cmd:Command):void
		{	
			var project:UserInfoObj = cmd.data as UserInfoObj;
			_mainView.playerObj = project;
			_mainView.upDataInfo();
		}
		private function upDataResouse(obj:UserInfoObj):void
		{
			_mainView.playerObj = obj;
			_mainView.upDataInfo();
		}
		private function onActionTypeChanged(cmd:SceneCommand):void
		{
			switch(cmd.data as String)
			{
				case ActionType.BUILD:
				case ActionType.MOVE:
				case ActionType.MOVETOBAG:
				case ActionType.REMOVE:
					_mainView.showAllUI(false);
					break;
				
				case ActionType.NORMAL:
					_mainView.showAllUI(true);
					break;
			}
		}
		
		private var _appNet:AppNet;
		private function startLogin(e:MainEvent):void{
			_login(AppConfig.getInstance().userId,AppConfig.getInstance().validateKey);
		}
		private function _login(str:String,sessionKey:String=""):void
		{
			_appNet = AppNet.getInstance();
			_appNet.connect(AppConfig.getInstance().socketHost,AppConfig.getInstance().socketPort);
			_appNet.login(str,sessionKey);
		}
	}
}